package de.bremen.bremenbybike.coordinates;

import java.text.DecimalFormat;
import android.util.FloatMath;

/**
 * A Gauss-Krueger position with x, y, and z coordinates.
 * 
 */
public final class GaussKruegerCoordinate {
    /*
     * consts
     */
    private static final DecimalFormat decFormat = new DecimalFormat("#.00");

    /*
     * members
     */
    /**
     * the x value
     */
    public float x;
    /**
     * the y value
     */
    public float y;
    /**
     * the z value
     */
    public float z;

    /*
     * getters & setters
     */
    /**
     * Set the x, y, and z coordinate.
     * 
     * @param x
     *            the x coordinate
     * @param y
     *            the y coordinate
     * @param z
     *            the z coordinate
     */
    public final void set(float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    /*
     * ctors
     */
    /**
     * Creates a position with x = y = z = 0.
     */
    public GaussKruegerCoordinate() {
        this(0, 0, 0);
    }

    /**
     * Creates a position with the given values.
     * 
     * @param x
     *            the x coordinate
     * @param y
     *            the y coordinate
     * @param z
     *            the z coordinate
     */
    public GaussKruegerCoordinate(float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    /**
     * Creates a position based on a given one.
     * 
     * @param position
     *            the position
     */
    public GaussKruegerCoordinate(GaussKruegerCoordinate position) {
        this.x = position.x;
        this.y = position.y;
        this.z = position.z;
    }

    /*
     * methods
     */
    /**
     * Calculates the squared distance to another position.
     * 
     * @param position
     *            the other position
     * @return the squared distance
     */
    public final float squaredDistanceXY(GaussKruegerCoordinate position) {
        float deltaX = this.x - position.x;
        float deltaY = this.y - position.y;
        return Math.abs(deltaX * deltaX + deltaY * deltaY);
    }


    /**
     * Copies the position members to another position.
     * 
     * @param position
     *            the position to which the members are copied
     */
    public final void copyMembers(GaussKruegerCoordinate position) {
        position.x = this.x;
        position.y = this.y;
        position.z = this.z;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object o) {
        if(o == null)
            return false;

        if(o == this)
            return true;

        if(!o.getClass().equals(getClass()))
            return false;

        GaussKruegerCoordinate position = (GaussKruegerCoordinate)o;
        return this.x == position.x && this.y == position.y && this.z == position.z;
    }

        /**
     * {@inheritDoc}
     */
    @Override
    public final String toString() {
        return decFormat.format(x) + ", " + decFormat.format(y) + ", " + decFormat.format(z);
    }
}
